<template>
  <div class="app-container">
    <el-form :model="teacher" label-width="80px">
      <el-form-item label="讲师姓名">
        <el-input v-model="teacher.name"/>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro"/>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career"/>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacher.level" placeholder="请选择讲师头衔">
          <el-option label="高级讲师" value="1"/>
          <el-option label="首席讲师" value="2"/>
        </el-select>
      </el-form-item>
      <!-- 讲师头像 -->
      <el-upload
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
        :action="uploadUrl"
        class="avatar-uploader">
        <img v-if="teacher.avatar" :src="teacher.avatar" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"/>
      </el-upload>
      <el-form-item label="讲师头衔">
        <el-input-number v-model="teacher.sort" :min="0" label="讲师排序"/>
      </el-form-item>
      <el-form-item label="入驻时间">
        <el-date-picker
          v-model="teacher.joinDate"
          type="date"
          placeholder="选择日期"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="onCancle">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import teacherApi from '@/api/teacherApi'
export default {
  data() {
    return {
      teacher: {},
      uploadUrl: 'http://localhost:9110/admin/oss/upload?module=avatar'
    }
  },
  created() {
    // 调用回显方法，使得查询的讲师信息回显在表单内
    if (this.$route.params.id) {
      console.log(this.$route.params.id)
      teacherApi.getTeacherById(this.$route.params.id).then(r => {
        this.teacher = r.data.item
      })
    }
  },
  methods: {
    // 新增讲师
    onSubmit() {
      if (this.$route.params.id) {
        teacherApi.updatedTeacher(this.teacher).then(r => {
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
          // 跳转到讲师列表页面
          this.$router.push('/teacher/list')
        })
      } else {
        teacherApi.addTeacher(this.teacher).then(r => {
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          // 跳转到讲师列表页面
          this.$router.push('/teacher/list')
        })
      }
    },
    onCancle() {
      this.teacher = {}
      this.$router.push('/teacher/list')
    },
    handleAvatarSuccess(res, file) {
      this.teacher.avatar = res.data.url
      this.$forceUpdate()
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
